package com.rxmvp.subscribers;
import com.loper7.base.utils.Logger;
import com.rxmvp.basemvp.BaseView;
import com.rxmvp.http.exception.ApiException;
import com.rxmvp.http.exception.ErrorType;
import com.rxmvp.utils.NetworkUtil;

/**
 *
 */
public abstract class RxSubscriber<T> extends BaseSubscriber<T> {

    private static final String TAG = RxSubscriber.class.getSimpleName();
    private BaseView mView;
//    private Context mContext;

    public RxSubscriber(BaseView mView) {

        this.mView = mView;
    }

    @Override
    public void onStart() {
        super.onStart();

//        // if  NetworkAvailable no !   must to call onCompleted
//        if (!NetworkUtil.isNetworkAvailable()) {
//            onCompleted();
//            if (mView != null)
//                mView.requestException(new ApiException(ErrorType.HTTP_ERROR, "无网络连接，请检查网络设置"));
//            // 无网络执行complete后取消注册防调用onError
//            if (!isUnsubscribed()) {
//                unsubscribe();
//            }
//        } else {
        if (mView != null)
            mView.showLoading();
//        }
    }

    @Override
    public void onCompleted() {
        if (mView != null)
            mView.hideLoading();
    }

    @Override
    protected void onError(ApiException ex) {
        if (mView != null) {
            try {
                mView.hideLoading();
                mView.requestException(ex);
            } catch (Exception e) {
                Logger.e(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onNext(T t) {
        if (mView == null)
            return;
    }
}
